package com.das.mechanic_base.base;

import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;

public abstract class X3BaseBottomSheetDialog extends BottomSheetDialog {

    protected Context mContext;


    public X3BaseBottomSheetDialog(@NonNull Context context) {
        super(context, R.style.MatchBottomSheetDialog);
        this.mContext = context;
    }

    public X3BaseBottomSheetDialog(@NonNull Context context, int style) {
        super(context,style);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        Window window = getWindow();
        if (window != null) {
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        }

        //按空白处不能取消动画
        setCanceledOnTouchOutside(!getDialogCancel());
        setCancelable(!getDialogCancel());
        initView();
    }

    public void changeCancel(boolean isCancel) {
        setCanceledOnTouchOutside(isCancel);
        setCancelable(isCancel);
    }

    @Override
    protected void onStart() {
        super.onStart();
        FrameLayout bottomSheet = findViewById(R.id.design_bottom_sheet);
        BottomSheetBehavior mDialogBehavior = BottomSheetBehavior.from(bottomSheet);
        mDialogBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        mDialogBehavior.setPeekHeight(X3ScreenUtils.getScreenHeight(mContext));
    }

    protected abstract boolean getDialogCancel();

    protected abstract void initView();

    protected abstract int getLayoutId();

    /**
     * 点击空白区域隐藏键盘.
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {  //把操作放在用户点击的时候
            View v = getCurrentFocus();      //得到当前页面的焦点,ps:有输入框的页面焦点一般会被输入框占据
            if (isShouldHideKeyboard(v, ev)) { //判断用户点击的是否是输入框以外的区域
                hideKeyboard(v.getWindowToken());   //收起键盘
                if (iOnHideKey != null) {
                    iOnHideKey.iOnHideKeyBoard();
                }
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    IOnHideKey iOnHideKey;

    public void setiOnHideKey(IOnHideKey iOnHideKey) {
        this.iOnHideKey = iOnHideKey;
    }


    /**
     * 隐藏软键盘监听
     */
    public interface IOnHideKey {
        void iOnHideKeyBoard();
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     *
     * @param token
     */
    protected void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     *
     * @param v
     * @param event
     * @return
     */
    protected boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {  //判断得到的焦点控件是否包含EditText
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],    //得到输入框在屏幕中上下左右的位置
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            System.out.println("输入框"+  " left = "+  left  +  " right = "+  right+ " top = "+  top+ " bottom = "+  bottom+" xx "+ event.getX() +"  yy " +  event.getY());

            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击位置如果是EditText的区域，忽略它，不收起键盘。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略
        return false;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = null;
        view = findViewById(viewId);
        return (T) view;
    }

}
